package com.gec.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.ITripItinerary;
import com.gec.model.system.ITripItineraryOrder;
import com.gec.model.system.ITripService;
import com.gec.model.system.ITripUser;
import com.gec.model.vo.ITripItineraryVo;
import com.gec.model.vo.ITripServiceVo;
import com.gec.system.common.Result;
import com.gec.system.service.ITripItineraryOrderService;
import com.gec.system.service.ITripItineraryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 出行方式表 前端控制器
 * </p>
 *
 * @author dafei
 * @since 2024-01-22
 */
@Api(tags = "出行方式管理")
@RestController
@RequestMapping("/system/itrItinerary")
public class ITripItineraryController {

    @Autowired
    private ITripItineraryService iTripItineraryService;


    private ITripItineraryOrderService iTripItineraryOrderService;

    // 分页 + 查询
    @ApiOperation("出行方式分页查询")
    @GetMapping("/{page}/{limit}")
    public Result findRoleByPageQuery(@PathVariable Long page,
                                      @PathVariable Long limit,
                                      ITripItineraryVo iTripItineraryVo) {
        IPage<ITripItinerary> page1 = new Page<>(page, limit);
        page1 = this.iTripItineraryService.selectPage(page1, iTripItineraryVo);
        return Result.ok(page1);
    }

    @ApiOperation("添加出行方式")
    @PostMapping("/addItinerary")
    public Result addItinerary(@RequestBody ITripItinerary iTripItinerary) {
//        添加编号唯一性判断
        ITripItinerary it = iTripItineraryService.selectByName(iTripItinerary.getTypeNo());
        if (it != null) {
            return Result.fail("该编号已存在");
        }
        this.iTripItineraryService.save(iTripItinerary);
        return Result.ok();
    }

//    用户选择出行方式，生成对应的出行方式订单表
    @ApiOperation("用户选择出行方式")
    @PostMapping("/chooseItinerary")
    public Result chooseItinerary(
            Long userId,
            Long id,
            Long number
    ) {
//      通过id查询当前出行方式所有信息
        ITripItinerary iti = iTripItineraryService.getById(id);
//         获取出行时间
        Date startTime = iti.getStartTime();
//          获取该出行方式的票数
        Long num = iti.getNumber();
        //判断当前票数
        boolean isTrue = num >= number;
        if (!isTrue) {

            return Result.fail("当前票数不足");
        }
        //减去用户买的票的数量
        iti.setNumber( num - number);
        iTripItineraryService.saveOrUpdate(iti);

//        用户选择出行方式前，需要判断出行方式出发时间与当前时间
        boolean flag = iTripItineraryService.isOverdue(startTime);
        if (!flag) {

            return Result.fail("该方式已经出发或到达目的地");
        }

//       用户选择好出行方式后，生成出行方式订单表
        ITripItineraryOrder it = new ITripItineraryOrder();
        it.setUserId(userId);
//        票数量*票价
        BigDecimal price =iti.getPrice().multiply(BigDecimal.valueOf(number));
        it.setPrice(price);
        it.setItineraryId(id);
        iTripItineraryOrderService.save(it);

        return Result.ok();

    }

    @ApiOperation("根据id去查询出行方式")
    // 根据id去
    @GetMapping("/findNodeById/{id}")
    public Result findNodeById(@PathVariable Long id) {
        ITripItinerary iTripItinerary = this.iTripItineraryService.getById(id);
        return Result.ok(iTripItinerary);
    }

    @ApiOperation(value = "修改出行方式")
    @PostMapping("/updateITripItinerary")
    public Result updateById(@RequestBody ITripItinerary iTripItinerary) {
        iTripItineraryService.updateById(iTripItinerary);
        return Result.ok();
    }

    @ApiOperation(value = "删除出行方式")
    @DeleteMapping("/removeItinerary/{id}")
    public Result removeITripService(@PathVariable Long id) {
        iTripItineraryService.removeById(id);
        return Result.ok();
    }

}

